<template>
  <el-form label-width="108px">
    <el-form-item label="选择任务" prop="taskId">
      <el-select
        v-model="formData.taskId"
        clearable
        placeholder="请选择"
        @change="(val) => taskChange(val, 'taskId')"
      >
        <el-option
          v-for="taskItem in taskList"
          :key="taskItem.id"
          :label="taskItem.name"
          :value="taskItem.id"
        />
      </el-select>
    </el-form-item>
  </el-form>
</template>
<script lang="ts" setup>
import { reactive, watch } from 'vue'
import { TaskApi } from '@/api/vmiot/task'

// 接收父组件传入的值
const props = defineProps<{
  modelValue: any
}>()

const emit = defineEmits(['update:modelValue', 'onChanged'])

const taskList = ref<any[]>([])

const formData = reactive<any>({})

const taskChange = (val: any, key: string) => {
  changed(val, key)
}

const getTaskList = async () => {
  const res = await TaskApi.getTaskPage({
    pageNo: 1,
    pageSize: 100,
    signalType: props.modelValue.signalType
  })
  taskList.value = res.list
}

watch(
  () => props.modelValue,
  (newVal) => {
    Object.assign(formData, newVal)
  },
  { immediate: true, deep: true }
)

onMounted(() => {
  getTaskList()
})

const changed = (val: any, key: string) => {
  emit('update:modelValue', { ...formData })
  emit('onChanged', { key, val })
}
</script>
<style lang="scss" scoped></style>
